<?php
namespace Home\Controller;
use Think\Controller;
class CalQuizController extends CommonController {

    //这些是测试函数 计算16pf

    public function index($uid)
    {

        if ($uid){
            $this->cal_16pf_o($uid);
            $this->cal_16pf_t($uid);
        }
        else{
            $this->cal_16pf_o();
            $this->cal_16pf_t();
        }

    }

    public function getQuestion()//uid为用户编号，或者临时测试序列号
    {
        $uid = I("post.uid");

        $questions = M('quiz_16pf_d_question');

        $answers = M('quiz_16pf_s_answer');
        $a = $answers->where(['uid' => $uid])->max('qid');//找最大题号
        if (!$a){
            //做过题的记录是空，获取第一道题

            $result = $questions->find($questions->min('qid'));//找第一道题
            $result['count'] =  $questions->count();
            $this->ajaxReturn($result);
        }
        else{
            if ($a + 1 > 186){
                $rs['code'] = -1;
                $rs['msg'] = '题目你都做完了，可以直接查看报告';
                $this->ajaxReturn($rs);
            }
            else{
                $result = $questions->find($a + 1);//做过的最大题号+1
                $result['count'] =  $questions->count();
                $this->ajaxReturn($result);
            }
        }

    }

    public function submitAnswer()
    {
        $qid = I("post.qid");
        $uid = I("post.uid");
        $answer = I("post.answer");
        $questions = M('quiz_16pf_s_answer');

        $data['uid'] = $uid;
        $data['qid'] = $qid;
        $data['answer'] = $answer;
        $questions->add($data);

        $rs['code'] = 1;
        $this->ajaxReturn($rs);

    }

    protected function cal_16pf_o($uid = 0)//16pf原始分计算方法
    {


        if (!$uid){
            $uid = parent::$uid;
        }



        $Dao = M();
        $qvalue_answer_list = $Dao->query("select quiz_16pf_s_answer.uid,quiz_16pf_s_answer.qid, quiz_16pf_s_answer.answer, quiz_16pf_d_qvalue.choice_1_value, quiz_16pf_d_qvalue.choice_2_value, quiz_16pf_d_qvalue.choice_3_value,quiz_16pf_d_qvalue.section from quiz_16pf_s_answer,quiz_16pf_d_qvalue
 where quiz_16pf_s_answer.qid = quiz_16pf_d_qvalue.qid and uid=".$uid.";");
        if (count($qvalue_answer_list) != 184){
            $rs['code'] = -1;
            $rs['msg'] = '问卷题数不对！';
            $this->ajaxReturn($rs);
            exit();
        }

        $t_result = [];
        $section = [];
        $ovalue = [];
        for ($i = 0; $i < count($qvalue_answer_list); $i++){
            $col = 'choice_'.$qvalue_answer_list[$i]['answer'].'_value';
            $t_result[$i] = [
                'uid' => $qvalue_answer_list[$i]['uid'],
                'qid' => $qvalue_answer_list[$i]['qid'],
                'value' => $qvalue_answer_list[$i][$col],
                'section' =>  $qvalue_answer_list[$i]['section']
            ];
            //找出答案对应的分值
            array_push($section, $qvalue_answer_list[$i]['section']);
            //提取因素
        }
        $section = array_unique($section);

        foreach($section as $s){
            $score = 0;
            foreach($t_result as $t){
                if ($t['section'] == $s){
                    $score = $score + $t['value'];
                }
            }
            $ovalue[$s] = [
              'uid' => $t_result[0]['uid'],
              'section' => $s,
              'value' => $score
            ];
        }//按照每个因素算分数总和

        $o = M('quiz_16pf_s_ovalue');
        foreach($ovalue as $v){
            $o->add($v);
        }//写入原始分表
        //var_dump($ovalue) ;
    }

    protected function cal_16pf_t($uid = 0)//16pf计算转换分
    {

        if (!$uid){
            $uid = parent::$uid;
        }

        $Dao = M('quiz_16pf_d_transfer_men');
        $t_table = $Dao->select();
        $Dao = M('quiz_16pf_s_ovalue');
        $ovalue = $Dao->where(["uid" => $uid])->select();
        $result = [];
        foreach($ovalue as $o){//对应每一条原始分
            foreach($t_table as $t){
                if ($t['section'] != $o['section'])//确定因素
                    continue;
                else{
                    for ($k = 1; $k <= count($t_table[0]) - 4; $k++){
                        $str =$t['t_'.$k];
                        preg_match_all('/([0-9]{1,}),([0-9]{1,}),([0-9]{1,})/', $str, $r);
                        $min = $r[1][0];//最低区间
                        $max = $r[2][0];//最高区间
                        $score = $r[3][0];//对应的分数

                        if ($o['value'] >= $min && $o['value'] <= $max){
                            $result['section_'.$o['section']] = $score;//在区间里，写入结果
                            break;
                        }
                        else
                            continue;//下一个区间
                    }
                    break;//下一个因素
                }
            }
        }

        $result['uid'] = $uid;
        $Dao = M('quiz_16pf_s_tvalue');
        $Dao->add($result);
        //var_dump($result);
        $rs['code'] = 1;
        $rs['msg'] = '16pf结果计算完毕，可以查看报告';
        $this->ajaxReturn($rs);
    }




}